<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

if (isset($_POST['load_files'])){

    $post_file_dir = $_POST['file_dir'];

    if ($_POST['delete_file'] != ''){
        $file = $_POST['delete_file'];
        unlink("$post_file_dir/$file");
        unlink("$post_file_dir/thumbs/$file");
    }

    foreach (scandir($post_file_dir) as $file){
        if (is_file("$post_file_dir/$file")){
            $browser_html .= "<div class='inactive' style='padding: 3px; width: 100px; display:inline-block; border: 1px solid black; text-align: center;' id='div-$file' onclick='select_file(\"$file\")'>$file</div>&nbsp;";
        }
    }

    echo $browser_html;
    die();
}


function get_filebrowser($div_id, $return_html, $file_dir, $relpath=""){

$browser_html = "<h3>"._("File Browser")."</h3>
<form name='refresh_files' action='' method='post' onsubmit='return AIM.submit(this, {onStart : startCallback, onComplete : reloadFiles})'>
<input type='hidden' name='load_files'/>
<input type='hidden' name='delete_file' id='delete_file'/>
<input type='hidden' name='file_dir' value='$file_dir'/>
<a href='#' onclick='upload_file()'>"._("Upload")."</a>
| <a href='#' onclick='delete_files()'>"._("Remove selected file")."</a>
| <a href='#' onclick='refreshFiles()'>"._("Refresh")."</a>

<br/>
<div id='files'></div>
<br/>
<a href='#' onclick='accept_file()'>"._("Accept")."</a>
| <a href='#' onclick='cancel_file()'>"._("Cancel")."</a>
</form>
";


$browser_html = str_replace("\n", "\\\n", $browser_html);

$upload_html = "<h3>"._("Upload file")."</h3>
		<form enctype='multipart/form-data' action='${relpath}admin/upload-file.php' method='post' onsubmit='return AIM.submit(this, {onStart : startCallback, onComplete : completeCallback})'>
		<div><label>File:</label> <input type='file' name='file' /></div>
		<div><input type='submit' value="._("Submit")." /></div>
        <input type='hidden' name='MAX_FILE_SIZE' value='100000000' />
        <input type='hidden' name='upload' value='file'/>
        <input type='hidden' name='fdir' value='$file_dir'/>
	</form>
    <a href='#' onclick='show_filebrowser()'>"._("Return")."</a>
</pre>
</div>";

$upload_html = str_replace("\n", "\\\n", $upload_html);

$imb_head = "
<script type='text/javascript' src='${relpath}ext_lib/webtoolkit.aim.js'></script>
	<script type='text/javascript'>
function startCallback() {
    // make something useful before submit (onStart)
    return true;
}

function completeCallback(response) {
    // make something useful after (onComplete)
    // document.getElementById('nr').innerHTML = parseInt(document.getElementById('nr').innerHTML) + 1;
    //document.getElementById('$div_id').innerHTML = response;
    document.getElementById('$div_id').innerHTML = \"$browser_html\";
    refreshfiles();

}

function reloadFiles(response) {
    document.getElementById('files').innerHTML = response;
}

function refreshFiles(){
    AIM.submit(document.forms['refresh_files'], {onStart : startCallback, onComplete : reloadFiles});
    document.forms['refresh_files'].submit();

}

var cb;
function show_filebrowser(callback){

    cb = callback;
	var thediv=document.getElementById('$div_id');
    thediv.style.display = \"\";
    thediv.innerHTML = \"$browser_html\";
    refreshFiles();
    return false;

}

function hide_filebrowser(){
	var thediv=document.getElementById('$div_id');
    thediv.style.display = \"\";
    thediv.innerHTML = \"$return_html\";
    return false;
}

function cancel_file(){
    hide_imagebrowser();
}

function accept_file(){

    var files = document.getElementsByTagName('div');
    var numOffiles = files.length;
    var strid = '';
    for(i=0;i<numOffiles;i++)
    {
        var div = files[i];
        if (div.className == 'active'){
            var strid = div.id;
            var strid = strid.replace('div-', '');


            break;
        }
    }
    hide_filebrowser();
    cb(strid);
}

function upload_file(){
	var thediv=document.getElementById('$div_id');
    thediv.style.display = \"\";
    thediv.innerHTML = \"$upload_html\";
	return false;
}

function unselect_all_files(){
    var files = document.getElementsByTagName('div');
    var numOffiles = files.length;
    for(i=0;i<numOffiles;i++)
    {
        var div = files[i];
        if (div.className == 'active' || div.className == 'inactive'){
            div.style.border='1px solid black';
            div.className = 'inactive';
            div.style.padding = '3px';
        }
    }
}

function select_file(file){
    unselect_all_files();
    var file = 'div-' + file;
    var div = document.getElementById(file);
    if (div.className == 'active'){
        div.style.border='1px solid black';
        div.className = 'inactive';
        div.style.padding = '3px'
    } else {
        div.style.border='2px solid black';
        div.className = 'active';
        div.style.padding = '1px';
    }

}

function delete_files(){
    var file = '';
    var images = document.getElementsByTagName('div');
    var numOfImages = images.length;
    for(i=0;i<numOfImages;i++)
    {
        var div = images[i];
        if (div.className == 'active'){
            var strid = div.id;
            file = strid.replace('div-', '');
            break;
        }
    }
    var field = document.getElementById('delete_file')
    if(confirm('"._("Are you sure you want to remove this file?")."')){
        field.value = file;
        refreshFiles();
        field.value = '';
    }
}
</script>";
return $imb_head;
}

?>
